<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/headLogin.jsp"%>

<body>
	<table style="width: 100%; height: 100%">
		<tr>
			<td style="width: 50%; vertical-align: top;">
				<form id="form1" name="form1" method="post" action="">
				<!-- 	<div id="p" class="easyui-panel" collapsible="true">
						名称:
						<input type="text" id="qrName" name="qrName" 
							style="width:100px"
							class="easyui-validatebox" />
						状态:
						<s:select id="qrState" class="easyui-combobox" 
							name="qrState" headerKey=""
							headerValue="%{pleaseSelect}" list="#request.roleStateList"
							listKey="peizhiValue" listValue="peizhiKey"
							style="width:100px" />
					</div> -->
				
				</form>
					<table id="roleDataGrid">
					</table>
			</td>
			<td style="width: 50%; vertical-align: top;">
				<iframe id="roleOperatorFrame" frameborder="0"
					style="width: 100%; height: 100%;" scrolling="yes">
				</iframe>
			</td>
		</tr>
	</table>
</body>
<script type="text/javascript">

$(document).ready(function() {
	setDataGridList();
});

//查询角色
function queryDataGrid() {
	var valid = $('#form1').form('validate');
	if (valid) {
		$('#roleDataGrid').datagrid('load', getJSONParam());
	}
}

var roleStateListJSON = eval('(${roleStateListJSON})');

//查询列表初始化
function setDataGridList() {
$('#roleDataGrid').datagrid( {
		url : 'role/getRoleList.action',
		title : "角色列表",
		fitColumns:true,nowrap:false,rownumbers:true,showFooter:false,pagination:true,striped:true,singleSelect:false,
		idField : 'id',
		frozenColumns : [ [ {
			field : 'ck',
			checkbox : true
		} ] ],
		tools: [{iconCls: 'icon-reload',handler: function(){$('#roleDataGrid').datagrid('reload');}}],
		toolbar : [ 
		<%if (SysAuthUtil.ifBtn(request, "roleAdd")) {%>
			{id:'btnadd',text:'增加',iconCls:'icon-add',handler:function(rowData){showAddRole(rowData);}},'-',
		<%}%>
			{id:'btnremove',text:'删除',iconCls:'icon-remove',handler:function(){removeBatch();}}
			,'-',{id:'btndisable',text:'禁用',iconCls:'icon-remove',handler:function(){disableBatch();}	}
			,'-',{id:'btnenabl',text:'启用',iconCls:'icon-add',handler:function(){enableBatch();}}
			//,'-',{id:'btnrefresh',text:'刷新',iconCls:'icon-reload',handler:function(){$('#roleDataGrid').datagrid('reload');}}
		],
		columns:[[{field:'id',hidden:true,sortable:true}
			,{field:'name',title:"名称",width:20,sortable:true}
			,{field:'descr',title:"描述",width:25,sortable:true}
			,{field:'state',title:"状态",width:8,sortable:true,formatter:function(val){
				for(var i = 0 ;i < roleStateListJSON.length; i++ ){
					console.log(roleStateListJSON[i]);
					if(roleStateListJSON[i].value == val){
						return roleStateListJSON[i].name;
					}
				}
				return val;
			}
			},{field:'createtime',title:"添加时间",width:10,sortable:true}
		]],
		onRowContextMenu : function(e, rowIndex, rowData) {
			e.preventDefault();
			$('#roleDataGrid').datagrid('unselectAll');
			$('#roleDataGrid').datagrid('selectRow', rowIndex);
			createColumnMenu(rowData);
			$('#tmenu').menu('show', {
				left : e.pageX,
				top : e.pageY
			});
		}
		,onDblClickRow: function (rowIndex, rowData) {
			$('#roleDataGrid').datagrid('unselectAll');
			$('#roleDataGrid').datagrid('selectRow', rowIndex);
			showRoleDetail(rowData);
		}  
	});
}

var tmenu;
function createColumnMenu(rowData) {
	if (tmenu != null) {
		$("#tmenu").remove();
	}
	var menuStr = '<div id="tmenu" style="width:135px;">';
	
	if (rowData.state == "0") {
		menuStr += '<div data-options="iconCls:\'icon-remove\'"id ="disable">禁用</div>';
	}else if (rowData.state == "2") {
		menuStr += '<div data-options="iconCls:\'icon-add\'"id ="enable">启用</div>';
	}

	menuStr += '<div data-options="iconCls:\'icon-edit\'" id ="update">修改</div>';
	menuStr += '<div data-options="iconCls:\'icon-remove\'" id ="remove">删除</div>';
	menuStr += '</div>';

	tmenu = $(menuStr).appendTo('body');
	//}
	tmenu.menu( {
		onClick : function(item) {
			if (item.id == 'add') {//增加
			 showAddRole(rowData);
		} else if (item.id == 'remove') {
			//删除
			$.messager.confirm("提示信息", "确定要删除吗?", function(r) {
				if (r) {
					deleteRole(rowData.id);
				//	$.post("doRoleRemove.action?operatorRoleId=" + rowData.id, callRemoveBack);
				}
			});
		} else if (item.id == 'update') {
			showRoleUpdate(rowData);
		} else if (item.id == 'disable') {
			$.messager.confirm("<s:text name='mes.message'/>", "确定要禁用吗?", function(r) {
				if (r) {
					updateRoleState(rowData.id,2);
				}
			});
		} else if (item.id == 'enable') {
			//启用
			$.messager.confirm("<s:text name='mes.message'/>", "确定要启用吗?", function(r) {
				if (r) {
					updateRoleState(rowData.id,0);
				}
			});
		}}
})	;
}

var updateRoleState = function(roleIds,updateState){
	$.axs("role/udpateRoleState.action", {'roleIds':roleIds,
		'roleState':updateState}, 
            function(data){
		if(data.status == 1){
			$.messager.alert("提示信息",
					data.msg, 'info', function() {
				$('#roleDataGrid').datagrid('load', getJSONParam());
			});
		}else{
			$.messager.alert("提示信息", data.msg, 'error');
		}
	});
};

var deleteRole = function(roleIds){
	$.axs("role/doRoleRemove.action", {'roleIds':roleIds}, function(data){
		if(data.status == 1){
			$.messager.alert("提示信息",data.msg, 'info', function() {
				$('#roleDataGrid').datagrid('load', getJSONParam());
				$('#roleDataGrid').datagrid('getSelections').length=0;  //重设选中行
				clearIframe();
			});
		}else{
			$.messager.alert("提示信息", data.msg, 'error');
		}
	});
};

function showAddRole(rowData){
	document.getElementById("roleOperatorFrame").src = "view/goViewWithoutData/role_roleAdd.action";
}

function showRoleUpdate(rowData){
	document.getElementById("roleOperatorFrame").src = "view/goRoleUpdate.action?roleId="+rowData.id;
}

function showRoleDetail(rowData){
	document.getElementById("roleOperatorFrame").src = "view/goRoleDetail.action?roleId="+rowData.id;
}
function goRoleDetail(rowData){
	addTab('角色详情', 'view/goRoleDetail.action?roleId='+rowData.id, '#tt', "parent.parent");
}

function doRoleAdd(){
	$.messager.alert('<s:text name="mes.message"/>', "已经打开新窗口添加角色，完成后点击确定刷新角色列表", 'info',function(){$('#roleDataGrid').datagrid('reload');});
	addTab("角色添加", "view/goViewWithoutData/role_roleAdd.action", "#tt", "parent");
}


//批量删除
function removeBatch() {
	var rows = $('#roleDataGrid').datagrid('getSelections');
	if (rows.length == 0) {
		$.messager.alert("提示信息", "请选择待操作的记录", "info");
	} else {
		$.messager.confirm("提示信息", "确定要删除选中的角色吗？（此操作无法回退）", function(r) {
			if (r) {
				var ids = getSelectIds(rows);
				deleteRole(ids);
			}
		});
	}
}

function disableBatch() {
	var rows = $('#roleDataGrid').datagrid('getSelections');
	if (rows.length == 0) {
		$.messager.alert("提示信息", "请选择待操作的记录", 'info');
	} else {
		$.messager.confirm("提示信息", "确定要禁用选中的角色吗？", function(r) {
			if (r) {
				var roleIds = getSelectIds(rows);
				updateRoleState(roleIds,2);
			}
		});
	}
}

function enableBatch() {
	var rows = $('#roleDataGrid').datagrid('getSelections');
	if (rows.length == 0) {
		$.messager.alert("提示信息", "请选择待操作的记录", 'info');
	} else {
		$.messager.confirm("提示信息", "确定要启用选中的角色吗？", function(r) {
			if (r) {
				var roleIds = getSelectIds(rows);
				updateRoleState(roleIds,0);
			}
		});
	}
}

var getSelectIds = function(rows){
	var ids = [];
	for ( var i = 0; i < rows.length; i++) {
		ids.push(rows[i].id);
	}
	return  ids.join(',');
	
};

function clearIframe(){
    var el =  document.getElementById('roleOperatorFrame');
    var iframe = el.contentWindow;
    if(el){
        el.src = 'about:blank';
        try{
            iframe.document.write('');
            iframe.document.clear();
        }catch(e){
        	
        }
        document.body.removeChild(el);
    }
}
</script>
</html>